<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>科泰聊天室</title>
  <link href="css/chat.css" rel="stylesheet">
  </link>
</head>

<body>
  <!-- TODO 根据HTML结构分析，定义出父组件和子组件结构 -->
  <div id="app">
    <h1 class="title center">科泰聊天室</h1>
    <my-footer></my-footer>
  </div>
  <template id="footer">
    <div class="chat-window center">
      <div class="chat-messages">
        <div v-for="(item, index) in items" :key="index"
          :class="{ 'message received-message': !item.flg, 'message sent-message': item.flg }">
          <div class="message-received">
            <img :src="item.img" class="avatar">
            <span class="message-content">{{item.title}}</span>
          </div>
        </div>
      </div>
      <footer-item @add-click="addClick"></footer-item>
    </div>
  </template>
  <template id="footerItem">
    <div class="chat-input"><input type="text" placeholder="请输入消息..." v-model="newTask">
      <button @click="emitAddClick">发送</button>
    </div>
  </template>

  <script src="js/vue.js"></script>
  <script src="js/axios.js"></script>

  <script>
    // 子组件定义
    var footerItem = {
      template: "#footerItem",
      data: function () {
        return {
          newTask: "" // 子组件可以有自己的新任务数据
        };
      },
      methods: {
        emitAddClick: function () {
          this.$emit("add-click", this.newTask); // 向父组件发送事件和数据
        }
      }
    };

    // 父组件定义
    var footer = {
      template: "#footer",
      data: function () {
        return {
          newTask: "",
          items: []
        };
      },
      methods: {
        addClick: function (newTask) {
          if (newTask.trim() !== "") {
            this.items.push({
              title: newTask,
              flg: true, // 表示发送的消息
              img: "images/avt-02.png"
            });
            this.newTask = ""; // 清空输入框
          }
        },
        newTiems() {
          let thiss = this;
          axios.get("js/chat-history.json").then(reson => {
            thiss.items = reson.data;
          })
        }
      },
        beforeCreate() {
        console.log("进入beforeCreate")
      },
      created() {
        this.newTiems();
        console.log("进入created")
      },
      beforeMount() {
        console.log("进入beforeMount")
      },
      mounted() {
        console.log("进入mounted")
      },
      beforeUpdate() {
        console.log("进入beforeUpdate")
      },
      updated() {
        console.log("进入updated")
      },
      beforeDestroy() {
        console.log("进入beforeDestroy")
      },
      destroyed() {
        console.log("进入destroyed")
      },
      components: {
        "FooterItem": footerItem
      }
    };

    // Vue 实例
    new Vue({
      el: "#app",
      components: {
        "MyFooter": footer
      },
      methods: {

      },
    
    });
  </script>
</body>

</html>